/**
 * @author lonun
 * @blog http://www.lonun.com
 */

var fs = require('fs');
var config = require(process.cwd() + '/config');

exports.index = function(form) {
	var cookies = form.reqCookies;
	return {
		title : '我的首页'
	};
};

exports.newPicture = function(form) {
	return {
		title : '上传图片'
	};
};
exports.add = function(form) {
	var temp = form.appContext.get('temp');
	console.log(temp);
	if (temp) {
		temp += 1;
		form.appContext.set('temp', temp);
		console.log(temp);
	} else {
		form.appContext.set('temp', 1)
	}
	return {
		t : temp
	};
};
exports.savePicture = function(form) {
	for ( var f in form.files) {
		var tmpF = form.files[f];
		var d = new Date();
		var tempName = tmpF.name;
		var suffix = '';
		if (tempName.indexOf('.') > -1) {
			suffix = tempName.substring(tempName.lastIndexOf('.'));
		}
		var fname = '' + d.getFullYear() + (d.getMonth() + 1) + d.getDay()
				+ d.getHours() + d.getMinutes() + d.getSeconds()
				+ Math.random().toString().substring(2);
		fs.renameSync(tmpF.path, config.uploadDir + '/' + fname + suffix);
	}
}

exports.testSession = function(form) {
	var s = form.getSession();
	var t = s.attr('test');
	if (!t) {
		s.attr('test', 1);
	} else {
		t += 1;
		s.attr('test', t);
	}
	return null;
}

exports.getSession = function(form) {
	var t = form.getSession().attr('test');
	if (!t) {
		form.session.attr('test', 1);
	} else {
		t += 1;
		form.session.attr('test', t);
	}
	return null;
}
